package com.education.welco.controller.app;

import com.education.common.pojo.AjaxResult;
import com.education.welco.pojo.SysSourcePay;
import com.education.welco.service.ISysSourcePayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * (用户资源收藏支付) 信息操作处理
 *
 * @author d
 * @date 2021-09-08
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/app/sysSource")
public class AppSysSourcePayController {

    @Autowired
    private ISysSourcePayService sysSourcePayService;

    private AjaxResult collect(SysSourcePay sysSourcePay, String type) {
        sysSourcePay.setCResourceType(type);
        List<SysSourcePay> sysSourcePays = sysSourcePayService.selectSysSourcePayList(sysSourcePay);
        if (sysSourcePays.size() > 0) {
            //判断字段中是否收藏
            SysSourcePay sourcePay = sysSourcePays.get(0);
            if (sourcePay.getNIsCollection() != null && sourcePay.getNIsCollection().equals(1)) {
                //已收藏，取消收藏
                sysSourcePay.setNIsCollection(2);
                sysSourcePayService.updateSysSourcePayCollectionStatus(sysSourcePay);
                return AjaxResult.success("取消收藏成功", 2);
            } else {
                //未收藏，进行收藏
                sysSourcePay.setNIsCollection(1);
                sysSourcePayService.updateSysSourcePayCollectionStatus(sysSourcePay);
                return AjaxResult.success("收藏成功", 1);
            }
        } else {
            //添加收藏
            sysSourcePay.setNIsPay(2);
            sysSourcePay.setNIsCollection(1);
            sysSourcePay.setCCreateBy(sysSourcePay.getCUserId().toString());
            sysSourcePay.setTCreateTime(new Date());
            sysSourcePayService.insertSysSourcePay(sysSourcePay);
            return AjaxResult.success("收藏成功", 1);
        }
    }


    /**
     * 新增保存 - app用户收藏免费公开课
     */
    @PostMapping("/add")
    public AjaxResult addSave(SysSourcePay sysSourcePay) {
        return collect(sysSourcePay, "1");
    }


    /**
     * 新增保存 - app用户收藏免费公开课
     */
    @PostMapping("/pc/add")
    public AjaxResult addSaveForPC(@RequestBody SysSourcePay sysSourcePay) {
        return collect(sysSourcePay, "1");
    }


    /**
     * 新增保存 - app用户收藏试题
     */
    @PostMapping("/addQuestion")
    public AjaxResult addQuestion(SysSourcePay sysSourcePay) {
        return collect(sysSourcePay, "2");
    }


    /**
     * 新增保存 - app用户收藏试题
     */
    @PostMapping("/addResource")
    public AjaxResult addResource(@RequestBody SysSourcePay sysSourcePay) {
        return collect(sysSourcePay, sysSourcePay.getCResourceType());
    }


    /**
     * 新增保存 - app用户收藏试题
     */
    @GetMapping("/addResourceForApp")
    public AjaxResult addResource(Integer cUserId, Integer cResourceId, String cResourceType) {
        SysSourcePay sysSourcePay = new SysSourcePay();
        sysSourcePay.setCUserId(cUserId);
        sysSourcePay.setCResourceId(cResourceId);
        return collect(sysSourcePay, cResourceType);
    }


}